//
//  CDImportTools.swift
//  PDF
//
//  Created by Kellv on 2024/4/20.
//

import Foundation
import ZLPhotoBrowser

class CDImportTools {
    static func importPhotoFromLibrary(viewController: CDBaseAllViewController, resultHandler:@escaping (([UIImage])->Void)) {
        CDAuthorizationTools.checkPermission(type: .library, presentVC: viewController) { flag, message in
            
            if flag {
                DispatchQueue.main.async {
//                    let freeCount = CDConfigFile.getIntValueFromConfigWith(key: .library_photo)
//                    if CDSignalTon.shared.vipType == .not && freeCount >= 1{
//                        self.present(self.vipVC, animated: true)
//                        return
//                    }
                    
                    let config = ZLPhotoConfiguration.default()
                    config.allowEditVideo = false
                    config.allowEditImage = false
                    config.allowSelectImage = true
                    config.allowSelectVideo = false
                    config.allowTakePhotoInLibrary = false
                    config.allowPreviewPhotos = false
                    config.maxSelectCount = 1


                    let uiconfig = ZLPhotoUIConfiguration.default()
                    uiconfig.showAddPhotoButton = false
                    let ps = ZLPhotoPreviewSheet()
                   
                    ps.selectImageBlock = {  results, isOriginal in
                        let images = results.map { model in
                            return model.image
                        }
                        resultHandler(images)
//                        if CDSignalTon.shared.vipType == .not && freeCount < 2{
//                            CDConfigFile.setIntValueToConfigWith(key: .library_photo, intValue: freeCount + 1)
//                        }
//                        DispatchQueue.global().async {
//                            CDSignalTon.shared.saveMedia(results, self.folder)
//                        }
//                        DispatchQueue.main.async {
//                            NotificationCenter.default.post(name: NSNotification.Name("addMediaSuccess"), object: nil)
//
//                            let sheet = UIAlertController(title: "Import Completed", message: "Your photos has been successfully imported into Hide photos & videos.\n\n Do you want to delete the imported photos from you photo library?", preferredStyle: .alert)
//                            sheet.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { (_) in
//                                CDSignalTon.shared.deleteSystemPhoto(results)
//                            }))
//                            sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
//                            self.present(sheet, animated: true, completion: nil)
//                        }
                    }
                    
                    ps.showPhotoLibrary(sender: viewController)
                }
            }
        }
    }
}
